package com.example.demo.service.exam.impl;

import com.example.demo.base.config.auth.UserInfoShareHolder;
import com.example.demo.domain.CombineClause;
import com.example.demo.domain.SingleClause;
import com.example.demo.domain.exam.ExamUserAbnormalBehavior;
import com.example.demo.exception.ExceptionUtils;
import com.example.demo.mapper.ExamUserAbnormalBehaviorMapper;
import com.example.demo.service.exam.ExamUserAbnormalBehaviorService;
import com.example.demo.service.impl.BaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ExamUserAbnormalBehaviorServiceImpl extends BaseServiceImpl<ExamUserAbnormalBehaviorMapper, ExamUserAbnormalBehavior> implements ExamUserAbnormalBehaviorService {

    private final ExamUserAbnormalBehaviorMapper mapper;

    @Override
    public void insert(ExamUserAbnormalBehavior entity) {
        Long userId = UserInfoShareHolder.getUserInfo().getUser().getId();
        entity.setUserId(userId);
        valid(entity);
        mapper.insert(entity);
    }

    @Override
    public List<ExamUserAbnormalBehavior> getByExamUserId(Long examId, Long userId) {
        if (userId == null) {
            throw ExceptionUtils.exception("用户标识不能为空");
        }
        if (examId == null) {
            throw ExceptionUtils.exception("考试标识不能为空");
        }
        return super.selectList(CombineClause.and(
                SingleClause.equal("exam_id", examId),
                SingleClause.equal("user_id", userId)
        ));
    }

    private void valid(ExamUserAbnormalBehavior entity) {
        Long userId = entity.getUserId();
        Long examId = entity.getExamId();
        if (userId == null) {
            throw ExceptionUtils.exception("用户标识不能为空");
        }
        if (examId == null) {
            throw ExceptionUtils.exception("考试标识不能为空");
        }
    }

}
